package com.epic.merge;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class DataBaseUtils implements Serializable {
    private Connection Connection;

    public static Connection getConnection() {
        String url = "";
        String username = "";
        String password = "";
        Properties properties = new Properties();
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader("/root/merge-files-1.0-SNAPSHOT/conf/jdbc.conf"));
            properties.load(bufferedReader);
            url = properties.getProperty("jdbc.url");
            username = properties.getProperty("jdbc.username");
            password = properties.getProperty("jdbc.password");
            Class.forName("org.postgresql.Driver");
        } catch (Exception e) {
            System.out.println(e);
        }
        try {
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isConnnetionValid(Connection conn) {
        if (conn == null)
            return false;
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select 1");
            rs.next();
            if (rs.getInt(1) == 1)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void closeConnection(Connection conn) {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException var3) {
                var3.printStackTrace();
            }
    }

    public static void closeStatement(Statement stmt) {
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException var3) {
                var3.printStackTrace();
            }
    }

    public static ArrayList<Map<String, Object>> query(Connection conn, String sql) {
        Statement statement = null;
        try {
            statement = conn.createStatement();
            return processPgData(statement.executeQuery(sql));
        } catch (Exception exception) {
            try {
                statement.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {}
        } finally {
            try {
                statement.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {}
        }
        return null;
    }

    public static ArrayList<Map<String, Object>> processPgData(ResultSet rs) {
        ArrayList<Map<String, Object>> result = new ArrayList<>();
        try {
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                Map<String, Object> curMap = new HashMap<>();
                for (int i = 1; i <= columnCount; i++)
                    curMap.put(md.getColumnName(i), rs.getObject(i));
                result.add(curMap);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
}

